/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.space.storey.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.space.constant.EdificeConstant;
import com.fuyao.cloud.space.dto.RoomStoreyDTO;
import com.fuyao.cloud.space.edifice.service.RoomEdificeService;
import com.fuyao.cloud.space.entity.RoomEdifice;
import com.fuyao.cloud.space.entity.RoomStorey;
import com.fuyao.cloud.space.storey.service.RoomStoreyService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 楼层管理
 *
 * @author 时间的朋友
 * @date 2021-11-11 15:30:25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/fyroomstorey" )
@Api(value = "fyroomstorey", tags = "楼层管理管理")
public class RoomStoreyController {

    private final RoomStoreyService fyRoomStoreyService;
    private final RoomEdificeService edificeService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param fyRoomStorey 楼层管理
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('storey_fyroomstorey_view')" )
    public R getFyRoomStoreyPage(Page page, RoomStorey fyRoomStorey) {
        return R.ok(fyRoomStoreyService.page(page, Wrappers.query(fyRoomStorey)));
    }

	/**
	 * 楼层树查询
	 * @param page 分页对象
	 * @param id
	 * @param affiliName
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/tree/page/{id}/{affiliName}" )
	@PreAuthorize("@pms.hasPermission('storey_fyroomstorey_view')" )
	public R getStoreTreePage(Page page, @PathVariable("id") Long id,@PathVariable("affiliName") String affiliName) {
		return R.ok(fyRoomStoreyService.getTreePage(page, id, affiliName));
	}

    /**
     * 通过id查询楼层管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('storey_fyroomstorey_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(fyRoomStoreyService.getById(id));
    }

    /**
     * 新增楼层管理
     * @param fyRoomStorey 楼层管理
     * @return R
     */
    @ApiOperation(value = "新增楼层管理", notes = "新增楼层管理")
    @SysLog("新增楼层管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('storey_fyroomstorey_add')" )
    public R save(@RequestBody RoomStorey fyRoomStorey) {
		fyRoomStoreyService.save(fyRoomStorey);
		return R.ok(fyRoomStorey);
    }

    /**
     * 修改楼层管理
     * @param fyRoomStorey 楼层管理
     * @return R
     */
    @ApiOperation(value = "修改楼层管理", notes = "修改楼层管理")
    @SysLog("修改楼层管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('storey_fyroomstorey_edit')" )
    public R updateById(@RequestBody RoomStorey fyRoomStorey) {
        return R.ok(fyRoomStoreyService.updateById(fyRoomStorey));
    }

    /**
     * 通过id删除楼层管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除楼层管理", notes = "通过id删除楼层管理")
    @SysLog("通过id删除楼层管理" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('storey_fyroomstorey_del')" )
    public R removeById(@PathVariable Long id) {
        return fyRoomStoreyService.batchDelete(Collections.singletonList(id));
    }

 	@ApiOperation(value = "批量删除楼层管理信息", notes = "批量删除楼层管理信息")
    @SysLog("批量删除楼层管理信息" )
    @DeleteMapping("/batchDelete" )
    @PreAuthorize("@pms.hasPermission('storey_fyroomstorey_del')" )
    public R batchDelete(@RequestBody List<Long> ids) {
        return fyRoomStoreyService.batchDelete(ids);
    }

    /**
     * 导出excel 表格
     * @param fyRoomStorey 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('storey_fyroomstorey_export')" )
    public List<RoomStorey> export(RoomStorey fyRoomStorey) {
        return fyRoomStoreyService.list(Wrappers.query(fyRoomStorey));
    }

	/**
	 * 批量新增楼层信息
	 * @param fyRoomStoreyDTO
	 * @return
	 */
	@ApiOperation(value = "批量新增楼层信息", notes = "批量新增楼层信息")
	@SysLog("批量新增楼层信息" )
	@PostMapping("/addList" )
	@PreAuthorize("@pms.hasPermission('storey_fyroomstorey_add_list')" )
	public R saves(@RequestBody RoomStoreyDTO fyRoomStoreyDTO) {
		return R.ok(fyRoomStoreyService.saveList(fyRoomStoreyDTO));
	}

	/**
	 * 返回树形菜单集合
	 * @return 树形菜单
	 */
	@GetMapping(value = "/tree")
	public R getTree() {
		return R.ok(fyRoomStoreyService.selectTree());
	}

	/**
	 * 返回树形菜单集合
	 * @return 树形菜单
	 */
	@GetMapping(value = "/validate/{edificeId}")
	public R validateEdifice(@PathVariable("edificeId") String edificeId) {
		String[] datas=edificeId.split("\\.");
		long count=0;
		if (datas.length==2){
			if (datas[0].equals(EdificeConstant.EDIFICE_TREE_NAME)){
				count= this.edificeService.count(Wrappers.<RoomEdifice>lambdaQuery().eq(RoomEdifice::getId,datas[1]));
			}else {
				return R.failed("请选择正确的楼栋信息");
			}
		}else {
			count= this.edificeService.count(Wrappers.<RoomEdifice>lambdaQuery().eq(RoomEdifice::getId,edificeId));
		}
		if (count>0){
			return R.ok();
		}else {
			return R.failed("不存在此楼栋信息，请联系管理员");
		}
	}

	/**
	 * 懒加载树
	 * @return 树集合
	 */
	@GetMapping(value = "/lazyTree/{edificeId}")
	public R getLazyTree(@PathVariable("edificeId") Long edificeId) {
		return R.ok(fyRoomStoreyService.lazyTree(edificeId));
	}

}
